package com.xl.beans;

import com.alibaba.druid.pool.DruidDataSource;
import com.xl.service.UserService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

//@Component 去掉注解可以在SpringConfig配置类加载进去
public class OtherBean {

    /*@Bean(name = "dataSource") //如果不设置name即默认取方法名
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        //设置4个基本参数
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/sys?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }*/

    @Bean(name = "dataSource") //如果不设置name即默认取方法名
    public DataSource dataSource(@Value("${jdbc.driver}") String driverClassName,
                                 @Value("${jdbc.url}") String url,
                                 @Value("${jdbc.username}") String username,
                                 @Value("${jdbc.password}") String password,
                                 @Qualifier("userService") UserService userService) {
        DruidDataSource dataSource = new DruidDataSource();
        System.out.println("driverClassName:"+driverClassName);
        System.out.println("url:"+url);
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        System.out.println("userService:"+userService);
        //设置4个基本参数
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
